import { MigrationInterface, QueryRunner } from 'typeorm';

export class AddMissingUserFields1625000000001 implements MigrationInterface {
  public async up(queryRunner: QueryRunner): Promise<void> {
    try {
      // 检查表是否存在
      const tableExists = await queryRunner.hasTable('users');
      if (!tableExists) {
        console.log('users 表不存在，创建基本表结构');
        await queryRunner.query(`
          CREATE TABLE users (
            id INT AUTO_INCREMENT PRIMARY KEY,
            username VARCHAR(255) UNIQUE,
            email VARCHAR(255) UNIQUE,
            password VARCHAR(255),
            phone VARCHAR(50) UNIQUE,
            nickname VARCHAR(255),
            avatar VARCHAR(255),
            createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            updatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
          )
        `);
      }

      // 检查并添加 gender 字段
      const hasGender = await queryRunner.hasColumn('users', 'gender');
      if (!hasGender) {
        await queryRunner.query(
          `ALTER TABLE users ADD COLUMN gender TINYINT DEFAULT 0`
        );
      }

      // 检查并添加 birthday 字段
      const hasBirthday = await queryRunner.hasColumn('users', 'birthday');
      if (!hasBirthday) {
        await queryRunner.query(
          `ALTER TABLE users ADD COLUMN birthday DATE NULL`
        );
      }

      // 检查并添加 province 字段
      const hasProvince = await queryRunner.hasColumn('users', 'province');
      if (!hasProvince) {
        await queryRunner.query(
          `ALTER TABLE users ADD COLUMN province VARCHAR(50) NULL`
        );
      }

      // 检查并添加 city 字段
      const hasCity = await queryRunner.hasColumn('users', 'city');
      if (!hasCity) {
        await queryRunner.query(
          `ALTER TABLE users ADD COLUMN city VARCHAR(50) NULL`
        );
      }

      // 检查并添加 status 字段
      const hasStatus = await queryRunner.hasColumn('users', 'status');
      if (!hasStatus) {
        await queryRunner.query(
          `ALTER TABLE users ADD COLUMN status TINYINT DEFAULT 1`
        );
      }

      // 检查并添加 isPhoneVerified 字段
      const hasIsPhoneVerified = await queryRunner.hasColumn('users', 'isPhoneVerified');
      if (!hasIsPhoneVerified) {
        await queryRunner.query(
          `ALTER TABLE users ADD COLUMN isPhoneVerified BOOLEAN DEFAULT false`
        );
      }

      // 检查并添加 isEmailVerified 字段
      const hasIsEmailVerified = await queryRunner.hasColumn('users', 'isEmailVerified');
      if (!hasIsEmailVerified) {
        await queryRunner.query(
          `ALTER TABLE users ADD COLUMN isEmailVerified BOOLEAN DEFAULT false`
        );
      }

      // 检查并添加 lastLoginIp 字段
      const hasLastLoginIp = await queryRunner.hasColumn('users', 'lastLoginIp');
      if (!hasLastLoginIp) {
        await queryRunner.query(
          `ALTER TABLE users ADD COLUMN lastLoginIp VARCHAR(50) NULL`
        );
      }
    } catch (error) {
      console.error('迁移出错:', error);
      throw error;
    }
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    // 不执行任何回滚操作，避免删除所有列的问题
    console.log('此迁移不支持回滚操作');
  }
}


